import 'package:fish_redux/fish_redux.dart';
import '../../../app_files.dart';

class OrderListItemModel {
  final String name;
  final String accountNo;
  final String orderID;
  final String userID;
  final String payTime;
  final String money;
  final String itemID;
  final String payOrderNo;
  final String status;
  final String payType;
  final String ctrlCode;
  final String cardNo;
  final String orderGas;
  final String lastRemain;
  final String orderRemain;
  final double orderPrice;
  final double orderOnePrice;
  final double orderTwoPrice;
  final double orderThreePrice;
  final double orderOneCubic;
  final double orderTwoCubic;
  final double orderThreeCubic;

  const OrderListItemModel({
    this.name,
    this.accountNo,
    this.payTime,
    this.money,
    this.itemID,
    this.payOrderNo,
    this.payType,
    this.status,
    this.ctrlCode,
    this.orderGas,
    this.lastRemain,
    this.orderRemain,
    this.orderID,
    this.userID,
    this.orderPrice,
    this.orderOnePrice,
    this.orderTwoPrice,
    this.orderThreePrice,
    this.orderOneCubic,
    this.orderTwoCubic,
    this.orderThreeCubic,
    this.cardNo,
  });

  bool get isSupportSave => ThemeConfig.supportCtrlCode.contains(ctrlCode);

  bool get isXABle => ctrlCode == 'XALYK';

  String get priceInfo {
    if (orderPrice != 0) return '正常气价：$orderPrice';
    String res = '';
    if (orderOneCubic > 0) res += '一阶单价/气量：$orderOnePrice / $orderOneCubic\r\n';
    if (orderTwoCubic > 0) res += '二阶单价/气量：$orderTwoPrice / $orderTwoCubic\r\n';
    if (orderThreeCubic > 0)
      res += '三阶单价/气量：$orderThreePrice / $orderThreeCubic\r\n';
    if (res.endsWith('\r\n')) {
      res = res.replaceRange(res.length - 2, null, '');
    }
    if (res.length == 0) return '-';
    return res;
  }

  String get orderStatus =>
      status == '1' ? '支付成功' : status == '2' ? '圈存成功' : '未支付';

  static fromJson(var json) {
    return OrderListItemModel(
      name: CommonUtils.checkNullToString(json['UserName']),
      accountNo: CommonUtils.checkNullToString(json['UserNo']),
      payTime: CommonUtils.checkNullToString(json['OrderAddTime']),
      money: CommonUtils.checkNullToString(json['OrderTotalFee']),
      payOrderNo: CommonUtils.checkNullToString(json['OrderId']),
      status: CommonUtils.checkNullToString(json['OrderStatus']),
      payType: CommonUtils.checkNullToString(json['OrderPayType']),
      ctrlCode: CommonUtils.checkNullToString(json['OrderMeterCtrlCode']),
      orderGas: CommonUtils.checkNullToString(json['OrderGasCubic']),
      lastRemain: CommonUtils.checkNullToString(json['OrderLastSurplus']),
      orderRemain: CommonUtils.checkNullToString(json['OrderSurplus']),
      orderID: CommonUtils.checkNullToString(json['OrderId']),
      userID: CommonUtils.checkNullToString(json['UserId']),
      cardNo: CommonUtils.checkNullToString(json['GasMeterCardId']),
      orderPrice:
          double.tryParse(CommonUtils.checkNullToString(json['OrderPrice'])) ??
              0.0,
      orderOnePrice: double.tryParse(
              CommonUtils.checkNullToString(json['OrderOnePrice'])) ??
          0.0,
      orderTwoPrice: double.tryParse(
              CommonUtils.checkNullToString(json['OrderTwoPrice'])) ??
          0.0,
      orderThreePrice: double.tryParse(
              CommonUtils.checkNullToString(json['OrderThreePrice'])) ??
          0.0,
      orderOneCubic: double.tryParse(
              CommonUtils.checkNullToString(json['OrderOneCubic'])) ??
          0.0,
      orderTwoCubic: double.tryParse(
              CommonUtils.checkNullToString(json['OrderTwoCubic'])) ??
          0.0,
      orderThreeCubic: double.tryParse(
              CommonUtils.checkNullToString(json['OrderThreeCubic'])) ??
          0.0,
    );
  }

  static fromPayResultJson(var json) {
    return OrderListItemModel(
      name: CommonUtils.checkNullToString(json['OrderUserName']),
      accountNo: CommonUtils.checkNullToString(json['OrderUserNo']),
      payTime: CommonUtils.checkNullToString(json['OrderAddTime']),
      money: CommonUtils.checkNullToString(json['OrderPayment']),
      payOrderNo: CommonUtils.checkNullToString(json['OrderId']),
      status: CommonUtils.checkNullToString(json['OrderStatus']),
      payType: CommonUtils.checkNullToString(json['OrderPayType']),
      ctrlCode: CommonUtils.checkNullToString(json['OrderMeterCtrlCode']),
      orderGas: CommonUtils.checkNullToString(json['OrderGasCubic']),
      lastRemain: CommonUtils.checkNullToString(json['OrderLastSurplus']),
      orderRemain: CommonUtils.checkNullToString(json['OrderSurplus']),
      orderID: CommonUtils.checkNullToString(json['OrderId']),
      userID: CommonUtils.checkNullToString(json['OrderUserId']),
      cardNo: CommonUtils.checkNullToString(json['OrderCardId']),
      orderPrice:
          double.tryParse(CommonUtils.checkNullToString(json['OrderPrice'])) ??
              0.0,
      orderOnePrice: double.tryParse(
              CommonUtils.checkNullToString(json['OrderOnePrice'])) ??
          0.0,
      orderTwoPrice: double.tryParse(
              CommonUtils.checkNullToString(json['OrderTwoPrice'])) ??
          0.0,
      orderThreePrice: double.tryParse(
              CommonUtils.checkNullToString(json['OrderThreePrice'])) ??
          0.0,
      orderOneCubic: double.tryParse(
              CommonUtils.checkNullToString(json['OrderOneCubic'])) ??
          0.0,
      orderTwoCubic: double.tryParse(
              CommonUtils.checkNullToString(json['OrderTwoCubic'])) ??
          0.0,
      orderThreeCubic: double.tryParse(
              CommonUtils.checkNullToString(json['OrderThreeCubic'])) ??
          0.0,
    );
  }
}

class OrderListViewState implements Cloneable<OrderListViewState> {
  List items;
  DateTime selectTime;

  int currentPage = 1;

  EasyRefreshController refreshController = EasyRefreshController();

  @override
  OrderListViewState clone() {
    return OrderListViewState()
      ..items = items
      ..selectTime = selectTime
      ..currentPage = currentPage
      ..refreshController = refreshController;
  }
}

OrderListViewState initState(Map<String, dynamic> args) {
  return OrderListViewState();
}
